public class DAY0321 {
    public ListNode reverseList(ListNode head) {
        ListNode curr=head;
        ListNode prev=null;
        while(curr!=null){
            ListNode next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        return prev;
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur=head;
        while(cur.next!=null){
            if(cur.next.val==cur.val){
                cur.next=cur.next.next;
            }else {
                cur=cur.next;
            }
        }
        return head;
    }
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length, n = matrix[0].length;
        int low = 0, high = m * n - 1;
        while (low <= high) {
            int mid = (high - low) / 2 + low;
            int x = matrix[mid / n][mid % n];
            if (x < target) {
                low = mid + 1;
            } else if (x > target) {
                high = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public int countNegatives(int[][] grid) {
        if(grid.length==0||grid[0].length==0){
            return 0;
        }
        int count=0;
        for (int i = 0, j = grid[0].length - 1; i < grid.length && j >= 0;) {
            if(grid[i][j]>=0){
                i++;
            }else{
                count+=grid.length-i;
                j--;
            }
        }
        return count;
    }
}
